<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Encoder extends MY_Controller {

	public function __construct()
    {
        parent::__construct();
    }
	
	public function index()
	{
		if(($this->is_logged_in())){
			$this->load->helper(array('form', 'url'));
			$data['title'] = 'Login';
			$this->load->view('templates/header', $data);
			$this->load->view('pages/encoder_homepage',array('error'=> ' ' ));
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	/* START OF FILE UPLOAD */
	function do_upload(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$config['upload_path'] = APPPATH . '/uploadbulk/';
			$config['allowed_types'] = 'csv';
		
			$this->load->library('upload', $config);

			if (!$this->upload->do_upload()){
				$error = array('error' => $this->upload->display_errors());
				$this->load->view('pages/encoder_homepage', $error);
			}else{
				$upload = array('upload_data' => $this->upload->data());
				$this->load->library('csvreader');
				$filePath = APPPATH . '/uploadbulk/try.csv';
				$data['csvData'] = $this->csvreader->parse_file($filePath);
	 
				$this->load->library('form_validation');
				$this->load->helper(array('url','form'));
				
				foreach($data['csvData'] as $item){
					$this->load->library('form_validation');
					/* Getting files */
					$stdno = $item['stdno'];
					$curriculum = $item['curriculum'];
					$reg_adviser = $item['regadviser'];
					$sp_adviser =$item['spadviser']; 
					echo $stdno . "</br>";
					$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
					/* Rules */
					$this->form_validation->set_rules('curriculum','curriculum','trim|callback_curriculum_null['.$stdno.']');
					$this->form_validation->set_rules('reg_adviser','reg_adviser','trim|callback_reg_adviser_null['.$stdno.']');
					$this->form_validation->set_rules('sp_adviser','sp_adviser','trim|callback_sp_adviser_null['.$stdno.']');
					
					if($this->form_validation->run() == FALSE) {
						echo "change";
						$data = array(
							'stdno' => $stdno,
							'curriculum' => $curriculum,
							'reg_adviser' => $reg_adviser,
							'sp_adviser' => $sp_adviser,
						);
						$this->load->model('encoder_model');
						$this->encoder_model->changeCurriculumAndAdviser($data);
						
						$uname = $_SESSION['username'];
						$message = $uname . " changed " . $stdno . "'s " . "curriculum and/or adviser.";
						$this->encoder_model->addToLog($message,$uname);
					}else {
						echo "add";
						$data = array(
							'stdno' => $stdno,
							'curriculum' => $curriculum,
							'reg_adviser' => $reg_adviser,
							'sp_adviser' => $sp_adviser,
						);
						$this->load->model('encoder_model');
						$this->encoder_model->addCurriculumAndAdviser($data);
					
						$uname = $_SESSION['username'];
						$message = $uname . " added " . $stdno . "'s " . "curriculum and adviser.";
						$this->encoder_model->addToLog($message,$uname);
					}
				}
				$data['title'] = 'Upload Successful';
				$this->load->view('templates/header', $data);
				$this->load->view('pages/upload_success',$upload);
				$this->load->view('templates/footer');
			}
		}else{
			redirect('login');
		}
	}
	/* END OF FILE UPLOAD */ 
	
	public function searchStudent(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->library('form_validation');
			
			$stdno = $this->input->post('stdno');
			$this->form_validation->set_rules('stdno','student number','trim|required|min_length[10]|max_length[10]|callback_stdno_check');
			
			if($this->form_validation->run() == FALSE){
				$data['title'] = 'Encoder';
				$this->load->view('templates/header', $data);
				$this->load->view('pages/encoder_homepage');
				$this->load->view('templates/footer');
			}else{ 
				$this->load->model('encoder_model');
				$curriculum = $this->encoder_model->getCurr($stdno);
				$result = $this->encoder_model->studAcadInfo($stdno);
				/* add student number to sessions. */
				$_SESSION['stdno'] = $stdno;
				redirect('/encoder/view/');
			}
		}else{
			redirect('login');
		}
	}
	
	public function loadCurriculumAndAdvisers(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->model('encoder_model');
			$result['inst_info'] = $this->encoder_model->getInstructors();
			$stdno = $_SESSION['stdno'];
			$result['stdno'] = $stdno;
			$result['current_curr'] = $this->encoder_model->getCurr($stdno);
			$result['curr_reg_adviser'] = $this->encoder_model->getCurrRegAdviser($stdno);
			$result['curr_sp_adviser'] = $this->encoder_model->getCurrSPAdviser($stdno);
			
			$data['title'] = "Curriculum and Adviser";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/encoder_curriculumandadvisers',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function loadGEplan(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->model('encoder_model');
			$result['ge_ah'] = $this->encoder_model->getGEah();
			$result['ge_ssp'] = $this->encoder_model->getGEssp();
			$result['ge_mst'] = $this->encoder_model->getGEmst();
			
			$stdno = $_SESSION['stdno'];
			$result['stdno'] = $stdno;
			$result['curr_ge'] = $this->encoder_model->getCurrGEs($stdno);
			
			$data['title'] = "GE Plan";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/encoder_geplan',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function loadElectives(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			
			$this->load->model('encoder_model');
			$stdno = $_SESSION['stdno'];
			$result['e_cs'] = $this->encoder_model->getElectiveCS();
			$result['e_am'] = $this->encoder_model->getElectiveAM();
			$result['e_dc'] = $this->encoder_model->getElectiveDC();
			$result['e_em'] = $this->encoder_model->getElectiveEM();
			$result['e_hum'] = $this->encoder_model->getElectiveHUM();
			$result['e_frm'] = $this->encoder_model->getElectiveFRM();
			$result['e_p'] = $this->encoder_model->getElectiveP();
			$result['e_ss'] = $this->encoder_model->getElectiveSS();
			$result['e_stat'] = $this->encoder_model->getElectiveSTAT();
			$result['e_ie'] = $this->encoder_model->getElectiveIE();
			$result['curr_elective'] = $this->encoder_model->getCurrElectives($stdno);
			$result['stdno'] = $stdno;
			$result['curr'] = $this->encoder_model->getCurr($stdno);
			
			$data['title'] = "List of Electives";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/encoder_electives',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function loadSubjectsTaken(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$result['stdno'] = $_SESSION['stdno'];
			
			$data['title'] = "Courses Taken";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/encoder_subjectstaken',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function loadCases(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			
			$stdno = $_SESSION['stdno'];
			$this->load->model('encoder_model');
			$result['cases_info'] = $this->encoder_model->getCases($stdno);
			$result['stdno'] = $stdno;
			
			$data['title'] = "University Cases";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/encoder_cases',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function addcase(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$stdno = $_SESSION['stdno'];
			$month = $this->input->post('month');
			$day = $this->input->post('day');
			$year = $this->input->post('year');
			$case = $this->input->post('case');
			$date = $month . '/' . $day . '/' . $year;
			
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			
			/* Rules */
			$this->form_validation->set_rules('case','case','trim|required|min_length[8]|max_length[40]');
			
			if($this->form_validation->run() == FALSE){
				$this->load->model('encoder_model');
				$result['cases_info'] = $this->encoder_model->getCases($stdno);
				$result['stdno'] = $stdno;
			
				$data['title'] = "University Cases";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/encoder_cases',$result);
				$this->load->view('templates/footer');
			}else{
				$data = array(
						'stdno' => $stdno,
						'date' => $date,
						'case' => $case,
						
					);
				$this->load->model('encoder_model');
				$this->encoder_model->addCase($data);
				
				$uname = $_SESSION['username'];
				$message = $uname . " added " . $stdno . " a university case.";
				$this->encoder_model->addToLog($message,$uname);
				
				redirect('/encoder/view/');
			}
		}else{
			redirect('login');
		}
	}

	public function gePlan(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$stdno = $_SESSION['stdno'];
			$ah = $this->input->post('ge_ah');
			$ssp = $this->input->post('ge_ssp');
			$mst = $this->input->post('ge_mst');
			
			$ahcount = 0;
			$sspcount = 0;
			$mstcount = 0;
			
			if(!empty($ah)){
				foreach($ah as $key  => $value)
					$ahcount = $ahcount+1;
			}
			if(!empty($ssp)){
				foreach($ssp as $key  => $value)
					$sspcount = $sspcount+1;
			}
			if(!empty($mst)){
				foreach($mst as $key  => $value)
					$mstcount = $mstcount+1;
			}
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			
			/* Rules */
			$this->form_validation->set_rules('ahform','AH','trim|callback_check_ah['.$ahcount.']');
			$this->form_validation->set_rules('sspform','SSP','trim|callback_check_ssp['.$sspcount.']');
			$this->form_validation->set_rules('mstform','MST','trim|callback_check_mst['.$mstcount.']');
			
			if($this->form_validation->run() == FALSE){
				$this->load->model('encoder_model');
				$result['ge_ah'] = $this->encoder_model->getGEah();
				$result['ge_ssp'] = $this->encoder_model->getGEssp();
				$result['ge_mst'] = $this->encoder_model->getGEmst();
				$result['stdno'] = $stdno;
				$result['curr_ge'] = $this->encoder_model->getCurrGEs($stdno);
				$data['title'] = "GE Plan";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/encoder_geplan',$result);
				$this->load->view('templates/footer');	
			}else{
				/* Check if first time to add or not. */
				$data = array('stdno' =>$stdno,);
				$this->load->model('encoder_model');
				$result = $this->encoder_model->checkIfInsertOrUpdate($data);
			
				if($result == true){	//No list of GE yet, so insert
					$data = array('stdno' =>$stdno,
							'ah' => $ah,
							'ssp' => $ssp,
							'mst' => $mst,
							);
					
					$this->load->model('encoder_model');
					$this->encoder_model->addGE($data);
					
					$uname = $_SESSION['username'];
					$message = $uname . " added " . $stdno . "'s " . "GE Plan of Course Work.";
					$this->encoder_model->addToLog($message,$uname);
					
					redirect('/encoder/view/');
				}else{
					$data = array('stdno' =>$stdno,
							'ah' => $ah,
							'ssp' => $ssp,
							'mst' => $mst,
							);
							
					$this->load->model('encoder_model');
					$this->encoder_model->deleteGE($data);
					$this->encoder_model->addGE($data);
					
					$uname = $_SESSION['username'];
					$message = $uname . " edited " . $stdno . "'s " . "GE Plan of Course Work.";
					$this->encoder_model->addToLog($message,$uname);
					
					redirect('/encoder/view/');
				}
			}
		}else{
			redirect('login');
		}
	}
	
	public function electives(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$stdno = $_SESSION['stdno'];
			$electives = $this->input->post('electives');
			
			$ecount = 0;
			if(!empty($electives)){
				foreach($electives as $key  => $value)
					$ecount = $ecount+1;
			}
				
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			/* Rules */
			$this->form_validation->set_rules('electivesform','curriculum','trim|callback_curriculum_null2['.$stdno.']');
			$this->form_validation->set_rules('electivesformcurr','Electives','trim|callback_check_electives['.$ecount.']');
			
			if($this->form_validation->run() == FALSE){
				$this->load->model('encoder_model');
				$result['e_cs'] = $this->encoder_model->getElectiveCS();
				$result['e_am'] = $this->encoder_model->getElectiveAM();
				$result['e_dc'] = $this->encoder_model->getElectiveDC();
				$result['e_em'] = $this->encoder_model->getElectiveEM();
				$result['e_hum'] = $this->encoder_model->getElectiveHUM();
				$result['e_frm'] = $this->encoder_model->getElectiveFRM();
				$result['e_p'] = $this->encoder_model->getElectiveP();
				$result['e_ss'] = $this->encoder_model->getElectiveSS();
				$result['e_stat'] = $this->encoder_model->getElectiveSTAT();
				$result['e_ie'] = $this->encoder_model->getElectiveIE();
				$result['stdno'] = $stdno;
				$result['curr'] = $this->encoder_model->getCurr($stdno);
				$result['curr_elective'] = $this->encoder_model->getCurrElectives($stdno);
				
				$data['title'] = "List of Electives";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/encoder_electives',$result);
				$this->load->view('templates/footer');
			}else{
				/* Check if first time to add or not. */
				$data = array('stdno' =>$stdno,);
				$this->load->model('encoder_model');
				$result = $this->encoder_model->checkIfInsertOrUpdateElective($data);
			
				if($result == true){	//No list of Electives yet, so insert
					$data = array(
							'stdno' =>$stdno,
							'electives' => $electives,
							);
					
					$this->load->model('encoder_model');
					$this->encoder_model->addElectives($data);
					
					$uname = $_SESSION['username'];
					$message = $uname . " added " . $stdno . "'s " . "List of Electives.";
					$this->encoder_model->addToLog($message,$uname);
					redirect('/encoder/view/');
				}else{
					$data = array(
							'stdno' =>$stdno,
							'electives' => $electives,
							);
							
					$this->load->model('encoder_model');
					$this->encoder_model->deleteElectives($data);
					$this->encoder_model->addElectives($data);
					$uname = $_SESSION['username'];
					$message = $uname . " edited " . $stdno . "'s " . "List of Electives.";
					$this->encoder_model->addToLog($message,$uname);
					redirect('/encoder/view/');
				}
			}
		}else{
			redirect('login');
		}
	}

	public function subjectsTaken(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$stdno = $_SESSION['stdno'];
			$year = $this->input->post('year');
			$sem = $this->input->post('sem');
			$choice = $this->input->post('option');
		
			if($choice == 1){	/*Add Subjects Taken*/
				$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
				/* add sem to sessions. */
				$_SESSION['sem'] = $sem;
				/* Rules */
				$this->form_validation->set_rules('year','year','trim|callback_year_format['.$year.']');
				$this->form_validation->set_rules('optionform','option','trim|callback_history_check['.$year.']');
				
				if($this->form_validation->run() == FALSE){
					$result['stdno'] = $stdno;
				
					$data['title'] = "Courses Taken";
					$this->load->view('templates/header', $data);
					$this->load->view('pages/encoder_subjectstaken',$result);
					$this->load->view('templates/footer');
				}else{
					$result['stdno'] = $stdno;
					$result['year'] = $year;
					$result['sem'] = $sem;
				
					$data['title'] = "Courses Taken";
					$this->load->view('templates/header', $data);
					$this->load->view('pages/encoder_addsubjectstaken',$result);
					$this->load->view('templates/footer');
				}
			}else{
				$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
				/* add sem to sessions. */
				$_SESSION['sem'] = $sem;
				/* Rules */
				$this->form_validation->set_rules('yearform','year','trim|callback_year_format['.$year.']');
				$this->form_validation->set_rules('optionform','option','trim|callback_history_check2['.$year.']');
				
				if($this->form_validation->run() == FALSE){
					$result['stdno'] = $stdno;
				
					$data['title'] = "Courses Taken";
					$this->load->view('templates/header', $data);
					$this->load->view('pages/encoder_subjectstaken',$result);
					$this->load->view('templates/footer');
				}else{
					$result['stdno'] = $stdno;
					$result['year'] = $year;
					$result['sem'] = $sem;
					
					$this->load->model('encoder_model');
					$cg = $this->encoder_model->getSubyearsem($result);
					$gs = $this->encoder_model->getGWAStanding($result);
					
					$i=1;
					foreach($cg as $item){
						$cinfo['a' . $i] = array(
								'course' => $item['course'],
								'grade' => $item['grade'], );
						$i = $i+1;
					}
					
					while($i != 8){
						$cinfo['a' . $i] = array(
								'course' => '',
								'grade' => '1.00', );
						$i = $i+1;
					}
					$cinfo['stdno'] = $stdno;
					$cinfo['year'] = $year;
					$cinfo['sem'] = $sem;
					$cinfo['gwa'] = $gs['gwa'];	
					$cinfo['standing'] = $gs['standing'];
					
					$data['title'] = "Courses Taken";
					$this->load->view('templates/header', $data);
					$this->load->view('pages/encoder_editsubjectstaken',$cinfo);
					$this->load->view('templates/footer');
				}
			}
		}else{
			redirect('login');
		}
	}
	
	public function addSubjectsTaken(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$stdno = $this->input->post('stdno');
			$year = $this->input->post('year');
			$sem = $this->input->post('sem');
			$course1 = $this->input->post('course1');
			$course2 = $this->input->post('course2');
			$course3 = $this->input->post('course3');
			$course4 = $this->input->post('course4');
			$course5 = $this->input->post('course5');
			$course6 = $this->input->post('course6');
			$course7 = $this->input->post('course7');
			$grade1 = $this->input->post('grade1');
			$grade2 = $this->input->post('grade2');
			$grade3 = $this->input->post('grade3');
			$grade4 = $this->input->post('grade4');
			$grade5 = $this->input->post('grade5');
			$grade6 = $this->input->post('grade6');
			$grade7 = $this->input->post('grade7');
			$gwa = $this->input->post('gwa');
			$standing = $this->input->post('standing');
			
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			/* Rules */
			$course1 = strtoupper($course1);
			$this->form_validation->set_rules('course1','course1','trim|callback_course_exist['.$course1.']');
			
			if($course2 != NULL){
				$course2 = strtoupper($course2);
				$this->form_validation->set_rules('course2','course2','trim|callback_course_exist['.$course2.']');
			}
			
			if($course3 != NULL){
				$course3 = strtoupper($course3);
				$this->form_validation->set_rules('course3','course3','trim|callback_course_exist['.$course3.']');
			}
			
			if($course4 != NULL){
				$course4 = strtoupper($course4);
				$this->form_validation->set_rules('course4','course4','trim|callback_course_exist['.$course4.']');
			}
			
			if($course5 != NULL){
				$course5 = strtoupper($course5);
				$this->form_validation->set_rules('course5','course5','trim|callback_course_exist['.$course5.']');
			}
			
			if($course6 != NULL){
				$course6 = strtoupper($course6);
				$this->form_validation->set_rules('course6','course6','trim|callback_course_exist['.$course6.']');
			}
			if($course7 != NULL){
				$course7 = strtoupper($course7);
				$this->form_validation->set_rules('course7','course7','trim|callback_course_exist['.$course7.']');
			}
			
			$this->form_validation->set_rules('gwa','gwa','trim|callback_gwa_format['.$gwa.']');
			
			if($this->form_validation->run() == FALSE){
				$result['stdno'] = $stdno;
				$result['year'] = $year;
				$result['sem'] = $sem;
				
				$data['title'] = "Courses Taken";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/encoder_addsubjectstaken',$result);
				$this->load->view('templates/footer');
			}else{
				$d['data1'] = array('course' => $course1,'grade' => $grade1,);
				$d['data2'] = array('course' => $course2,'grade' => $grade2,);
				$d['data3'] = array('course' => $course3,'grade' => $grade3,);
				$d['data4'] = array('course' => $course4,'grade' => $grade4,);
				$d['data5'] = array('course' => $course5,'grade' => $grade5,);
				$d['data6'] = array('course' => $course6,'grade' => $grade6,);
				$d['data7'] = array('course' => $course7,'grade' => $grade7,);
				
				$d['data8'] = array(
						'stdno' => $stdno,
						'year' => $year,
						'sem' => $sem,
						'gwa' => $gwa,
						'standing' => $standing,
					);
				
				$this->load->model('encoder_model');
				$this->encoder_model->addSubjects($d);
				
				$uname = $_SESSION['username'];
				$message = $uname . " added " . $stdno . "'s " . "subjects taken for year " . $year . "(" . $sem . ")";
				$this->encoder_model->addToLog($message,$uname);
				redirect('/encoder/view/');
			}
		}else{
			redirect('login');
		}
	}
	
	public function editSubjectsTaken(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$stdno = $this->input->post('stdno');
			$year = $this->input->post('year');
			$sem = $this->input->post('sem');
			$course1 = $this->input->post('course1');
			$course2 = $this->input->post('course2');
			$course3 = $this->input->post('course3');
			$course4 = $this->input->post('course4');
			$course5 = $this->input->post('course5');
			$course6 = $this->input->post('course6');
			$course7 = $this->input->post('course7');
			$grade1 = $this->input->post('grade1');
			$grade2 = $this->input->post('grade2');
			$grade3 = $this->input->post('grade3');
			$grade4 = $this->input->post('grade4');
			$grade5 = $this->input->post('grade5');
			$grade6 = $this->input->post('grade6');
			$grade7 = $this->input->post('grade7');
			$gwa = $this->input->post('gwa');
			$standing = $this->input->post('standing');
			
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			/* Rules */
			$course1 = strtoupper($course1);
			$this->form_validation->set_rules('course1','course1','trim|callback_course_exist['.$course1.']');
			
			if($course2 != NULL){
				$course2 = strtoupper($course2);
				$this->form_validation->set_rules('course2','course2','trim|callback_course_exist['.$course2.']');
			}
			
			if($course3 != NULL){
				$course3 = strtoupper($course3);
				$this->form_validation->set_rules('course3','course3','trim|callback_course_exist['.$course3.']');
			}
			
			if($course4 != NULL){
				$course4 = strtoupper($course4);
				$this->form_validation->set_rules('course4','course4','trim|callback_course_exist['.$course4.']');
			}
			
			if($course5 != NULL){
				$course5 = strtoupper($course5);
				$this->form_validation->set_rules('course5','course5','trim|callback_course_exist['.$course5.']');
			}
			
			if($course6 != NULL){
				$course6 = strtoupper($course6);
				$this->form_validation->set_rules('course6','course6','trim|callback_course_exist['.$course6.']');
			}
			if($course7 != NULL){
				$course7 = strtoupper($course7);
				$this->form_validation->set_rules('course7','course7','trim|callback_course_exist['.$course7.']');
			}
			
			$this->form_validation->set_rules('gwaform','gwa','trim|callback_gwa_format['.$gwa.']');
			
			if($this->form_validation->run() == FALSE){
				$result['stdno'] = $stdno;
				$result['year'] = $year;
				$result['sem'] = $sem;
				
				$this->load->model('encoder_model');
				$cg = $this->encoder_model->getSubyearsem($result);
				$gs = $this->encoder_model->getGWAStanding($result);
					
				$i=1;
				foreach($cg as $item){
					$cinfo['a' . $i] = array(
						'course' => $item['course'],
						'grade' => $item['grade'], );
						$i = $i+1;
				}
					
				while($i != 8){
					$cinfo['a' . $i] = array(
							'course' => '',
							'grade' => '1.00', );
					$i = $i+1;
				}
				$cinfo['stdno'] = $stdno;
				$cinfo['year'] = $year;
				$cinfo['sem'] = $sem;
				$cinfo['gwa'] = $gs['gwa'];	
				$cinfo['standing'] = $gs['standing'];
				
				$data['title'] = "Courses Taken";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/encoder_editsubjectstaken',$cinfo);
				$this->load->view('templates/footer');
				
			}else{
				$d['data1'] = array('course' => $course1,'grade' => $grade1,);
				$d['data2'] = array('course' => $course2,'grade' => $grade2,);
				$d['data3'] = array('course' => $course3,'grade' => $grade3,);
				$d['data4'] = array('course' => $course4,'grade' => $grade4,);
				$d['data5'] = array('course' => $course5,'grade' => $grade5,);
				$d['data6'] = array('course' => $course6,'grade' => $grade6,);
				$d['data7'] = array('course' => $course7,'grade' => $grade7,);
				
				$d['data8'] = array(
						'stdno' => $stdno,
						'year' => $year,
						'sem' => $sem,
						'gwa' => $gwa,
						'standing' => $standing,
					);
				
				$this->load->model('encoder_model');
				$this->encoder_model->deleteSubjects($d['data8']);
				$this->encoder_model->addSubjects($d);
			
				$uname = $_SESSION['username'];
				$message = $uname . " edited " . $stdno . "'s " . "subjects taken for year " . $year . "(" . $sem . ")";
				$this->encoder_model->addToLog($message,$uname);
				
				redirect('/encoder/view/');
			}	
		}else{
			redirect('login');
		}
	}
	
	public function view(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$stdno = $_SESSION['stdno'];
			$data = array('stdno' => $stdno,);
			$this->load->model('encoder_model');
			$result = $this->encoder_model->studAcadInfo($stdno);

			$data['title'] = "Student's Academic Information";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/encoder_viewBasicAcadInfo',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function clearGEList(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$stdno = $_SESSION['stdno'];
			$data = array('stdno' => $stdno,);
			
			$this->load->model('encoder_model');
			$this->encoder_model->deleteGE($data);
			
			$uname = $_SESSION['username'];
			$message = $uname . " deleted all GE Courses of " . $stdno . ".";
			$this->encoder_model->addToLog($message,$uname);
				
			redirect('/encoder/view/');
		}else{
			redirect('login');
		}
	}
	
	public function clearElectives(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$stdno = $_SESSION['stdno'];
			$data = array('stdno' => $stdno,);
			
			$this->load->model('encoder_model');
			$this->encoder_model->deleteElectives($data);
			
			$uname = $_SESSION['username'];
			$message = $uname . " deleted all electives of " . $stdno . ".";
			$this->encoder_model->addToLog($message,$uname);
			
			redirect('/encoder/view/');
		}else{
			redirect('login');
		}
	}
	
	public function check_ah($ah,$ahcount){
		if(($this->is_logged_in())){
			if($ahcount != 5){
				$this->form_validation->set_message('check_ah', 'AH selected must be equal to 5');
				return FALSE;
			}else return TRUE;
		}else{
			redirect('login');
		}
	}
	
	public function check_ssp($ssp,$sspcount){
		if(($this->is_logged_in())){
			if($sspcount != 5){
				$this->form_validation->set_message('check_ssp', 'SSP selected must be equal to 5');
				return FALSE;
			}else return TRUE;
		}else{
			redirect('login');
		}
	}
	
	public function check_mst($mst,$mstcount){
		if(($this->is_logged_in())){
			if($mstcount != 5){
				$this->form_validation->set_message('check_mst', 'MST selected must be equal to 5');
				return FALSE;
			}else return TRUE;
		}else{
			redirect('login');
		}
	}
	
	public function check_electives($electives,$ecount){
		if(($this->is_logged_in())){
			$stdno = $_SESSION['stdno'];
			$curr = $this->encoder_model->getCurr($stdno);
			
			if(($curr == 2008) && $ecount != 3){
				$this->form_validation->set_message('check_electives', 'Curriculum is 2008. Must have 3 electives.');
				return FALSE;
			}else if(($curr == 2011) && $ecount != 4){
				$this->form_validation->set_message('check_electives', 'Curriculum is 2011. Must have 4 electives.');
				return FALSE;
			}else return TRUE;
		}else{
			redirect('login');
		}
	}
	
	public function curriculumAndAdviser(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			/* Get input of the user. */
			$stdno = $_SESSION['stdno'];
			$curriculum = $this->input->post('curriculum');
			$reg_adviser = $this->input->post('reg_adviser');
			$sp_adviser = $this->input->post('sp_adviser');
				
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			
			/* Rules */
			$this->form_validation->set_rules('curriculum','curriculum','trim|callback_curriculum_null['.$stdno.']');
			$this->form_validation->set_rules('reg_adviser','reg_adviser','trim|callback_reg_adviser_null['.$stdno.']');
			$this->form_validation->set_rules('sp_adviser','sp_adviser','trim|callback_sp_adviser_null['.$stdno.']');
			
			if($this->form_validation->run() == FALSE) {
				$data = array(
					'stdno' => $stdno,
					'curriculum' => $curriculum,
					'reg_adviser' => $reg_adviser,
					'sp_adviser' => $sp_adviser,
				);
				$this->load->model('encoder_model');
				$this->encoder_model->changeCurriculumAndAdviser($data);
				
				$uname = $_SESSION['username'];
				$message = $uname . " changed " . $stdno . "'s " . "curriculum and/or adviser.";
				$this->encoder_model->addToLog($message,$uname);
				redirect('/encoder/view/');
			}else {
				$data = array(
					'stdno' => $stdno,
					'curriculum' => $curriculum,
					'reg_adviser' => $reg_adviser,
					'sp_adviser' => $sp_adviser,
				);
				$this->load->model('encoder_model');
				$this->encoder_model->addCurriculumAndAdviser($data);
				/* Add curriculum to sessions */
				$_SESSION['curr'] = $curriculum;
				
				$uname = $_SESSION['username'];
				$message = $uname . " added " . $stdno . "'s " . "curriculum and adviser.";
				$this->encoder_model->addToLog($message,$uname);
				
				redirect('/encoder/view/');
			}
		}else{
			redirect('login');
		}
	}
	
	public function curriculum_null($curriculum,$std_no){
		if(($this->is_logged_in())){
			$this->load->model('encoder_model');
			$result = $this->encoder_model->check_curriculum_null($std_no);
			if($result == false){
				return false;
			}else return true;
		}else{
			redirect('login');
		}
	}
	
	public function year_format($year,$year){
		if(($this->is_logged_in())){
			if (preg_match('^\d{4}-\d{4}^',$year))
				return true;
			else{
				$this->form_validation->set_message('year_format', 'Invalid Year Format.');
				return false;
			}
		}else{
			redirect('login');
		}
	}
	
	public function gwa_format($gwa,$gwa){
		if(($this->is_logged_in())){
			if (preg_match('^\d{1}\.\d{2}^',$gwa))
				return true;
			else{
				$this->form_validation->set_message('gwa_format', 'Invalid GWA format.');
				return false;
			}
		}else{
			redirect('login');
		}
	}
	
	public function curriculum_null2($curriculum,$std_no){
		if(($this->is_logged_in())){
			$this->load->model('encoder_model');
			$result = $this->encoder_model->check_curriculum_null($std_no);
			
			if($result == false)
				return true;
			else{
				$this->form_validation->set_message('curriculum_null2', 'Cannot add Electives unless curriculum is specified.');
				return false;
			}
		}else{
			redirect('login');
		}
	}
	
	public function reg_adviser_null($reg_adviser,$std_no){
		if(($this->is_logged_in())){
			$this->load->model('encoder_model');
			/* If reg_adviser field is null, return true else false. */
			$result = $this->encoder_model->check_reg_adviser_null($std_no);
			if($result == false) return false;
			else return true;
		}else{
			redirect('login');
		}
	}
	
	public function sp_adviser_null($sp_adviser,$std_no){
		if(($this->is_logged_in())){
			$this->load->model('encoder_model');
			/* If sp_adviser field is null, return true else false. */
			$result = $this->encoder_model->check_sp_adviser_null($std_no);
			if($result == false) return false;
			else return true;
		}else{
			redirect('login');
		}
	}
	
	public function BackViewBasicAcadInfo($stdno){
		if(($this->is_logged_in())){
			$this->load->model('encoder_model');
			$result = $this->encoder_model->studAcadInfo($stdno);

			$data['title'] = "Student's Academic Information";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/encoder_viewBasicAcadInfo',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function stdno_check($stdno){
		if(($this->is_logged_in())){
			$this->load->model('student_model');
			$result = $this->student_model->check_stdno($stdno); 
			$result2 = $this->student_model->check_activeaccount($stdno); 
			
			if (preg_match('^\d{4}-\d{5}^',$stdno))
				$result3 = true;
			
			if (($result && $result2 && $result3) == true) return true;
			else{
				$this->form_validation->set_message('stdno_check', 'Error. Either wrong student number format or account is inactive or does not exist.');
				return false;
			}
		}else{
			redirect('login');
		}
	}
	
	public function course_exist($course,$course){
		if(($this->is_logged_in())){
			$this->load->model('encoder_model');
			$result = $this->encoder_model->checkif_courseexist($course);
			
			if ($result == true) return true;
			else{
				$this->form_validation->set_message('course_exist', 'Course does not exist.');
				return false;
			}
		}else{
			redirect('login');
		}
	}
	
	public function history_check($year,$year){
		if(($this->is_logged_in())){
			$stdno = $_SESSION['stdno'];
			$sem = $_SESSION['sem'];
			
			$this->load->model('encoder_model');
			$result = $this->encoder_model->yearsemexist($stdno,$year,$sem);

			if ($result == true) return true;
			else{
				$this->form_validation->set_message('history_check', 'Cannot Add. Record exists.');
				return false;
			}
		}else{
			redirect('login');
		}
	}

	
	public function history_check2($year,$year){
		if(($this->is_logged_in())){
			$stdno = $_SESSION['stdno'];
			$sem = $_SESSION['sem'];
			
			$this->load->model('encoder_model');
			$result = $this->encoder_model->yearsemexist($stdno,$year,$sem);

			if ($result == false) return true;
			else{
				$this->form_validation->set_message('history_check2', 'Cannot Edit. Record does not exist.');
				return false;
			}
		}else{
			redirect('login');
		}
	}
	
	public function logout(){
		if(($this->is_logged_in())){
			session_destroy();
			redirect('login');
		}else{
			redirect('login');
		}
	}
}
